package com.kristin.io;


import java.io.*;
import java.util.Properties;
import java.util.Set;

/**
 * @ClassName IOTest
 * @Description TODO
 * @Author Kristin
 * @Date 2020/09/03 09:20:31
 * @Version 1.0.0
 */
public class IOTest {

    public static void main(String[] args) throws IOException, ClassNotFoundException {
//        new Copy().copy("D:\\fs\\www\\sc\\back\\in\\10\\2020\\08\\31\\1\\snmh_custinf_log20200831_1.DAT", "D:\\fs\\www\\sc\\back\\in\\10\\2020\\08\\31\\1\\snmh_custinf_log20200831_1_bak.DAT");
//        new Copy().copy("D:\\迅雷下载\\CentOS-7-x86_64-DVD-1908.iso", "D:\\迅雷下载\\CentOS-7-x86_64-DVD-1908-bak.iso");
//        FileOutputStream fos = new FileOutputStream("D:\\fs\\www\\sc\\back\\in\\10\\2020\\08\\31\\1\\1.txt");
//        fos.write("割肝模式".getBytes());
//        fos.close();


//        FileInputStream fis = new FileInputStream("D:\\fs\\www\\sc\\back\\in\\10\\2020\\08\\31\\1\\1.txt");
//        byte[] arr = new byte[1024];
//        int len;
//        while ((len=fis.read(arr))!=-1){
////            System.out.print((char)len);
//            System.out.println(new String(arr,0,len));
//        }

//            FileReader fr = new FileReader("D:\\fs\\www\\sc\\back\\in\\10\\2020\\08\\31\\1\\1.txt");
//            char[] c = new char[1024];
//            int len = 0;
//            while((len = fr.read(c)) != -1 ) {
//                System.out.print(new String(c , 0 , len));
//            }

//        FileReader fr = new FileReader("D:\\fs\\www\\sc\\back\\in\\10\\2020\\08\\31\\1\\1.txt");
//        FileWriter fw = new FileWriter("D:\\fs\\www\\sc\\back\\in\\10\\2020\\08\\31\\1\\2.txt");
//        int len = 0;
//        while((len = fr.read()) != -1) {
//            fw.write(len);
//        }
//        fw.flush();
//        fr.close();
//        fw.close();
//        FileInputStream fis = new FileInputStream("D:\\fs\\www\\sc\\back\\in\\10\\2020\\08\\31\\1\\snmh_custinf_log20200831_1.DAT");
//        BufferedInputStream bufferedInputStream = new BufferedInputStream(fis);
//        bufferedInputStream.read
//        FileReader fileReader = new FileReader("D:\\fs\\www\\sc\\back\\in\\10\\2020\\08\\31\\1\\snmh_custinf_log20200831_1.DAT");
//        BufferedReader bufferedReader = new BufferedReader(fileReader);
//        String line = null;
//        while ((line = bufferedReader.readLine()) != null){
//            System.out.println(line);
//        }

        // 诗篇排序
//        BufferedReader br = new BufferedReader(new FileReader("D:\\fs\\www\\sc\\back\\in\\10\\2020\\08\\31\\1\\1.txt"));
//        List<String> list = new ArrayList<>();
//        String line;
//        while ((line = br.readLine()) != null) {
//            list.add(line);
//        }
//        Collections.sort(list);
//        BufferedWriter bw = new BufferedWriter(new FileWriter("D:\\fs\\www\\sc\\back\\in\\10\\2020\\08\\31\\1\\2.txt"));
//
//        for(String str : list) {
//            bw.write(str);
//            bw.newLine();
//            System.out.println(str);
//        }
//        br.close();
//        bw.flush();
//        bw.close();

        // 字节转码
//        FileInputStream fos = new FileInputStream("D:\\fs\\www\\sc\\back\\in\\10\\2020\\08\\31\\1\\编码解码.txt");
//        InputStreamReader reader = new InputStreamReader(fos, "GB2312");
//        BufferedReader reader1 = new BufferedReader(reader);
//        String line;
//        while ((line = reader1.readLine()) != null) {
//            System.out.println(line);
//        }

        // 控制台输入
//        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
//        String line;
//        while ((line = reader.readLine()) != null && !"q".equals(line)) {
//            System.out.println(line);
//        }


        //对象写入
//        Student stu = new Student();
//        stu.setName("张三");
//        stu.setAge(26);
//        ObjectOutputStream objectInputStream = new ObjectOutputStream(new FileOutputStream("D:\\fs\\www\\sc\\back\\in\\10\\2020\\08\\31\\1\\对象序列化.txt"));
//        objectInputStream.writeObject(stu);
//        objectInputStream.close();

        // 反序列化
//        ObjectInputStream objectInputStream = new ObjectInputStream(new FileInputStream("D:\\fs\\www\\sc\\back\\in\\10\\2020\\08\\31\\1\\对象序列化.txt"));
//        Student stu = (Student)objectInputStream.readObject();
//        System.out.println();
//        objectInputStream.close();

        // 打印流
//        BufferedReader br=new BufferedReader(new FileReader("D:\\fs\\www\\sc\\back\\in\\10\\2020\\08\\31\\1\\1.txt"));
//        PrintStream ps = new PrintStream("D:\\fs\\www\\sc\\back\\in\\10\\2020\\08\\31\\1\\printStream.txt");
//        String line;
//        while((line = br.readLine()) != null) {
//            ps.println(line);
//        }
//        ps.flush();
//        br.close();
//        ps.close();

        // 加载属性
        Properties pro = new Properties();
        pro.load(new FileReader("D:\\fs\\www\\sc\\back\\in\\10\\2020\\08\\31\\1\\properties.txt"));
        Set<String> propertiesNames = pro.stringPropertyNames();
        for(String key : propertiesNames) {
            System.out.println( key + " --" + pro.getProperty(key));
        }
    }
}

class Student implements Serializable{

    private static final long serialVersionUID = 1231231231123123000L;

    private String name;

    private int age;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }
}


class Copy {

    /**
     * 1024数组 缓冲流 4.34G 用时:79274 毫秒
     * 1024数组 字节流 4.34G 用时:103171 毫秒
     * 1024*8 数组 缓冲流 4.34G 用时:79395 毫秒
     *
     * @param sourcePath
     * @param targetPath
     * @throws IOException
     */
    public void copy(String sourcePath, String targetPath) throws IOException {
        long start = System.currentTimeMillis();
        // 输入流
        FileInputStream fis = new FileInputStream(sourcePath);
        BufferedInputStream bis = new BufferedInputStream(fis);

        // 输出流
        FileOutputStream fos = new FileOutputStream(targetPath);
        BufferedOutputStream bos = new BufferedOutputStream(fos);
        long fileSize = fis.getChannel().size();
//        System.out.println("文件大小:" + fileSize  + "B") ;
        byte[] arr = new byte[1024 * 8];
//        long planSize = 0;
        int len = 0;
        while ((len = bis.read(arr)) != -1) {
            bos.write(arr, 0, len);
//            planSize += len;
            // 复制进度展示
//            String rank = (planSize*100/fileSize + "") + "%";
//            System.out.println("复制进度" + rank);
        }
        bis.close();
        bos.close();
        long end = System.currentTimeMillis();
        System.out.println("用时:" + (end - start) + " 毫秒");
    }

}
